package edu.drexel.cs575.taskManager.gui.controller.users;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import edu.drexel.cs575.taskManager.common.ServiceFactory;
import edu.drexel.cs575.taskManager.common.serviceDropSpot.user.UsermgmtCaller;
import edu.drexel.cs575.taskManager.gui.Helper;
import edu.drexel.cs575.taskManager.gui.controller.tests.Contact;
import edu.drexel.cs575.taskManager.gui.model.users.LoginCredentials;
import edu.drexel.cs575.taskManager.gui.model.users.User;

@Controller
public class UserController {
	
	@RequestMapping("createUser")
	public ModelAndView createUser(){
        return new ModelAndView("createAccount", "command", new User());
	}
	
	@RequestMapping(value = "/createUser", method = RequestMethod.POST)
    public ModelAndView createUser(@ModelAttribute("user")
                            User user, BindingResult result){	    
	    String creationResult = "did not succeed";
	    
	    if(!user.getUserName().isEmpty() && !user.getPassword().isEmpty()){
	        try{
	        	UsermgmtCaller userService = ServiceFactory.getUserService();
	        	userService.adduser(user.getUserName(), user.getPassword(), "");
	        	creationResult = "succeeded";
	        }
	        catch(Throwable t){
	        	
	        }
	    }
         
        return new ModelAndView("creationResult", "message", creationResult);
	}
	
	@RequestMapping(value = "/loginAction", method = RequestMethod.POST)
    public ModelAndView login(HttpServletResponse response, @ModelAttribute("login")
                            LoginCredentials login, BindingResult result){	
		String loginResult = "Login failed.  Please try again.";
		
		boolean isAuthenticated = false;
		
		try{
			UsermgmtCaller userService = ServiceFactory.getUserService();
			isAuthenticated = userService.authUser(login.getUserName(), login.getPassword(), "");
		}
		catch(Throwable t){
			
		}
		
		if(isAuthenticated == true){
			response.addCookie(new Cookie("userName", login.getUserName()));
			loginResult = "Login successful.  Click on one of the links in the menu to continue.";
		}
		
        return new ModelAndView("loginResult", "message", loginResult);		
	}
	
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response){
		String userName = Helper.getUserName(request);
		
		if(Helper.isLoggedIn(request)){
			Cookie cookie = new Cookie("userName", null); // Not necessary, but saves bandwidth.
			cookie.setMaxAge(0); // Don't set to -1 or it will become a session cookie!
			response.addCookie(cookie);
		}

		
        return new ModelAndView("index", "command", new LoginCredentials());
	}
	
	@RequestMapping("users")
	public ModelAndView userDetails(HttpServletRequest request){
		String message = "Please login to see user information";
		if(Helper.isLoggedIn(request)){
			message = Helper.getUserName(request);
		}
		return new ModelAndView("userDetail", "userName", message);
	}
	
	public void userUpdate(Object whatGotUpdated){
		UsermgmtCaller userService = ServiceFactory.getUserService();
	}
}
